using System;
using System.IO;
using System.Web;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace BEGalleryExtension
{
    public class ImportHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.Write(String.Format("Process Started at {0}<br />", DateTime.Now.ToLongTimeString()));
            PhotoInformation pinfo = new PhotoInformation();

            DirectoryInfo basePath = new DirectoryInfo(context.Server.MapPath("~/App_Data/gallery"));
            string[] folders = Directory.GetDirectories(basePath.FullName, "*", SearchOption.AllDirectories);
            foreach (string folder in folders)
                ProcessFiles(pinfo, folder);

            pinfo.Save();
            context.Response.Write(String.Format("Process Finished at {0}<br />", DateTime.Now.ToLongTimeString()));
            context.Response.Redirect("~/");
        }

        private void ProcessFiles(PhotoInformation pinfo, string folder)
        {
            DirectoryInfo dinfo = new DirectoryInfo(folder);
            FileInfo[] files = dinfo.GetFiles("*.jpg");

            foreach (FileInfo file in files)
                pinfo.insertPhoto(dinfo, file.Name);
        }

        public bool IsReusable
        {
            get { return true; }
        }
    }
}
